home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 April / EnigmA AMIGA RUN 17 (1997)(G.R. Edizioni)(IT)[!][issue 1997-04][EAR-CD].iso / EARCD / text / hyper / hsc_source.lha / hsc / source / ugly / ustring.c < prev    next >
C/C++ Source or Header  |  1996-09-04  |  8KB  |  355 lines

  1. /*
  2.  * ugly/ustring.c
  3.  *
  4.  * ugly string functions
  5.  *
  6.  * Copyright (C) 1993,94,95,96  Thomas Aglassinger
  7.  *
  8.  * This program is free software; you can redistribute it and/or modify
  9.  * it under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 2 of the License, or
  11.  * (at your option) any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  * GNU General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * along with this program; if not, write to the Free Software
  20.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  *
  22.  * updated: 30-Jul-1996
  23.  * created: 31-Jul-1993
  24.  */
  25.  
  26. #include <ctype.h>
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. #include <string.h>
  30.  
  31. #include "utypes.h"
  32. #include "umemory.h"
  33.  
  34. #define NOEXTERN_UGLY_USTRING_H
  35. #include "ustring.h"
  36.  
  37. static char ch2str_buffer[2];   /* internal buffer for _ch2str() */
  38. static char num2str_buffer[10]; /* internal buffer for _long2str() */
  39.  
  40. /*
  41.  * strclone
  42.  *
  43.  * create a clone of a string
  44.  *     (copy of string using its own memory area)
  45.  *
  46.  * params: *oldstr...ptr to string to clone
  47.  * result: ptr to copy of string;
  48.  * errors: out of memory: returns NULL
  49.  *
  50.  */
  51. STRPTR ugly_strclone(CONSTRPTR oldstr, STRPTR file, ULONG line)
  52. {
  53.     STRPTR newstr = NULL;
  54.  
  55.     if (oldstr) {
  56.  
  57.         /* alloc mem for clone */
  58. #if DEBUG_UGLY_MEMORY
  59.         newstr = ugly_malloc_tracking(strlen(oldstr) + 1, file, line);
  60. #else
  61.         newstr = umalloc(strlen(oldstr) + 1);
  62. #endif
  63.  
  64.         if (newstr)             /* alloc sucessful? */
  65.             strcpy(newstr, oldstr);     /* Y-> copy data */
  66.  
  67.     }
  68.     return (newstr);            /* return result */
  69. }
  70.  
  71. /*
  72.  * upstr: convert a string to upper case
  73.  *
  74.  * params: *s...string to convert
  75.  *
  76.  */
  77. STRPTR upstr(STRPTR s)
  78. {
  79.     STRPTR s_old = s;
  80.  
  81.     if (s)
  82.         for (; *s != '\0'; s++)
  83.             *s = toupper(*s);
  84.  
  85.     return s_old;
  86. }
  87.  
  88. /*
  89.  * upstrcmp
  90.  *
  91.  * string compare, ignore case
  92.  *
  93.  * params: *s1, *s2...ptr to strings to compare
  94.  * result: -1: s1 < s2
  95.  *          0: s1 = s2
  96.  *          1: s1 > s2
  97.  *
  98.  */
  99. int upstrcmp(CONSTRPTR s1, CONSTRPTR s2)
  100. {
  101. #define QUICKY 1
  102. #if !QUICKY
  103.     int equal;                  /* result of _strcp() */
  104. #endif
  105.     unsigned char c1, c2;       /* chars currently comparing */
  106.     size_t i = 0;               /* string index counter */
  107.  
  108.     do {
  109.         c1 = toupper(s1[i]);
  110.         c2 = toupper(s2[i]);
  111.         i++;
  112.     } while (c1 && c2 && (c1 == c2));
  113.  
  114. #if QUICKY
  115.     return(c2-c1);
  116. #else
  117.     if (c1 < c2)
  118.         equal = -1;             /* s1 < s2 */
  119.     else if (c1 > c2)
  120.         equal = +1;             /* s1 > s2 */
  121.     else
  122.         equal = 0;              /* s1 = s2 */
  123.  
  124.     return (equal);             /* return result */
  125. #endif
  126. }
  127.  
  128. /*
  129.  * upstrncmp
  130.  *
  131.  * string compare, ignore case, max. n chars
  132.  *
  133.  * params: *s1, *s2...ptr to strings to compare
  134.  *         n..........max. nr. of chars to compare
  135.  * result: -1: s1 < s2
  136.  *          0: s1 = s2
  137.  *          1: s1 > s2
  138.  *
  139.  */
  140. int upstrncmp(CONSTRPTR s1, CONSTRPTR s2, size_t n)
  141. {
  142.     int equal;                  /* result of _strcp() */
  143.     unsigned char c1, c2;       /* char of string currently comparing */
  144.     size_t i = 0;               /* string index counter */
  145.  
  146.     do {
  147.         c1 = toupper(s1[i]);
  148.         c2 = toupper(s2[i]);
  149.         i++;
  150.     } while (c1 && c2 && (c1 == c2) && (i < n));
  151.  
  152.     if (c1 < c2)
  153.         equal = -1;             /* s1 < s2 */
  154.     else if (c1 > c2)
  155.         equal = +1;             /* s1 > s2 */
  156.     else
  157.         equal = 0;              /* s1 = s2 */
  158.  
  159.     return (equal);             /* return result */
  160. }
  161.  
  162. /*
  163.  * freestr
  164.  *
  165.  * free memory used by a string
  166.  *
  167.  * params: s..string to free
  168.  *
  169.  * NOTE: strings created with strclone() or reallocstr
  170.  *       should be freed with this function instead of
  171.  *       ufree(). this will avoid problems when using
  172.  *       the memory-tracking feature of ugly.o
  173.  */
  174. void ugly_freestr(STRPTR s, STRPTR file, ULONG line)
  175. {
  176. #if DEBUG_UGLY_MEMORY
  177.     ugly_free(s, file, line);
  178. #else
  179.     ufree(s);
  180. #endif
  181. }
  182.  
  183. /*
  184.  * reallocstr
  185.  *
  186.  * free memory used by string, clone new data
  187.  *
  188.  * params:oldstr...prt to ptr of old string
  189.  *          *newstr...ptr to new string data
  190.  * errors: out of mem: returns NULL (old data are lost)
  191.  *
  192.  * IMPORTANT: old string has to be initialies like "oldstr = NULL;" before !!
  193.  *            first call like "reallocstr( &oldstr, "<new data>" );"
  194.  */
  195. void ugly_reallocstr(STRPTR * oldstr, CONSTRPTR newstr, STRPTR file, ULONG line)
  196. {
  197. #if DEBUG_UGLY_MEMORY
  198.     ugly_freestr(*oldstr, file, line);  /* free old string */
  199.     *oldstr = ugly_strclone(newstr, file, line);        /* clone new string */
  200. #else
  201.     ufree(*oldstr);             /* free old string */
  202.     *oldstr = strclone(newstr); /* clone new string */
  203. #endif
  204. }
  205.  
  206. /*
  207.  * ch2str
  208.  *
  209.  * convert single char to zero terminated string
  210.  *
  211.  * params: ch...char to convert
  212.  * result: ptr to internal buffer, where a string containning
  213.  *         "<ch>\0" is located.
  214.  *
  215.  * USAGE : buffer is rewritten at next call of _chstr(), so
  216.  *         be sure to clone string if you need it for a longer
  217.  *         time.
  218.  *         _ch2str() is mostly designed for parameter conversion
  219.  *         on string funtions, e.g s = strclone( ch2str( 'x' ) );
  220.  *
  221.  */
  222. STRPTR ch2str(const char ch)
  223. {
  224.     ch2str_buffer[0] = ch;
  225.     ch2str_buffer[1] = '\0';
  226.  
  227.     return ch2str_buffer;
  228. }
  229.  
  230. /*
  231.  * ustrrpbrk
  232.  *
  233.  * searches for last occurence of a char of _set in _str
  234.  *
  235.  * params: str...string to examine
  236.  *         set...chars to search for
  237.  * result: ptr where char of _set occured in _str
  238.  *         or NULL if none occurence found
  239.  *
  240.  * NOTE:   this is a clone of none-ansi-c function _strrpbrk()
  241.  *
  242.  */
  243. STRPTR ustrrpbrk(CONSTRPTR str, CONSTRPTR set)
  244. {
  245.     size_t i;
  246.     STRPTR result = NULL;
  247.  
  248.     if (str) {
  249.  
  250.         i = strlen(str) - 1;
  251.  
  252.         while ((i) && (strchr(set, str[i]) == NULL))
  253.             i--;
  254.  
  255.         if (strchr(set, str[i]))
  256.             result = (STRPTR) & (str[i]);
  257.     }
  258.     return result;
  259.  
  260. }
  261.  
  262. /*
  263.  * str2long
  264.  *
  265.  * convert string expression to long value
  266.  *
  267.  * params: s....string to convert
  268.  *         num..ptr to destination long var
  269.  * result: TRUE, if sucessful
  270.  * errors: returen FALSE
  271.  *
  272.  */
  273. BOOL str2long(STRPTR s, LONG * num)
  274. {
  275.     BOOL conv_ok = FALSE;
  276.  
  277.     if (sscanf(s, "%d", (int *) num)) {
  278.  
  279.         conv_ok = TRUE;
  280.  
  281.     }
  282.     return conv_ok;
  283. }
  284.  
  285. /*
  286.  * long2str
  287.  *
  288.  * convert long value to string expresion
  289.  *
  290.  * params: num...value to convert
  291.  * result: ptr to converted string
  292.  * errors: returns NULL
  293.  *
  294.  */
  295. STRPTR long2str(LONG num)
  296. {
  297.     STRPTR result_str = NULL;
  298.  
  299.     if (sprintf(num2str_buffer, "%d", (int) num)) {
  300.  
  301.         result_str = num2str_buffer;
  302.  
  303.     }
  304.     return result_str;
  305. }
  306.  
  307. /*
  308.  * strenum
  309.  *
  310.  * find a substring in an enumerator string
  311.  *
  312.  * params: str...substring to search for
  313.  *         set...set of legal substrings
  314.  *         sep...substring separator (eg "|")
  315.  * result:  0..not found
  316.  *         >0..index of substring found
  317.  *         <0..out of mem
  318.  * errors: out of mem: returns -1
  319.  *
  320.  * example: enumstr( "sepp", "hugo|sepp|resi", '|', STEN_CASE ) -> 2
  321.  *
  322.  * NOTE: calls strtok()
  323.  */
  324. LONG strenum(STRPTR str, STRPTR set, char sep, BYTE options)
  325. {
  326.     STRPTR s = strclone(set);
  327.     LONG found = 0;
  328.  
  329.     if (s) {
  330.  
  331.         STRPTR nxtstr = strtok(s, ch2str(sep));
  332.         LONG count = 1;
  333.  
  334.         while (!found && nxtstr) {
  335.  
  336.             if (options & STEN_NOCASE) {
  337.                 if (!upstrcmp(str, nxtstr))
  338.                     found = count;
  339.             } else if (!strcmp(str, nxtstr))
  340.                 found = count;
  341.  
  342.             count++;
  343.             nxtstr = strtok(NULL, ch2str(sep));
  344.  
  345.         }
  346.  
  347.         ufreestr(s);
  348.  
  349.     } else
  350.         found = -1;
  351.  
  352.     return (found);
  353. }
  354.  
  355.